package main

import (
	"encoding/json"
	"fmt"
	"os"
	"strings"
)

type Teacher struct {
	Name  string
	Age   int    `json:"age"`
	Class string `json:"className"`
}

//如果数据已经存在于内存中使用 marshal 方法
//如果数据来源于流 请使用encode

func main() {
	t1 := Teacher{
		Name:  "tch1",
		Age:   10,
		Class: "class4",
	}
	encoder := json.NewEncoder(os.Stdout)
	encoder.Encode(t1)

	fmt.Println("==========================")

	var p1 Teacher
	rdr := strings.NewReader(`{"Name":"tch1","age":10,"className":"class4"}`)
	json.NewDecoder(rdr).Decode(&p1)
	fmt.Println(p1)
}
